﻿Imports ACompany.OrderProcessing.Infrastructure

Public Class Customer
    Inherits BusinessBase(Of Customer)
    Implements IAggregateRoot
    Implements IEntity

    Private _address As Address
    Private _Name As String
    Private _Email As String
    Private _orders As IList(Of Order)

    Public Sub New()
        ' Default Constructor required for NHibernate
    End Sub

    Public Property Address() As Address
        Get
            Return _address
        End Get
        Set(ByVal value As Address)
            _address = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Public Property Email() As String
        Get
            Return _Email
        End Get
        Set(ByVal value As String)
            _Email = value
        End Set
    End Property

    Public Function Orders() As IList(Of Order)
        Return _orders
    End Function

    Protected Overrides Sub validate()

        If Me.Address Is Nothing Then
            Me.BrokenRules.Add(New BrokenRule("Address", "A Customer must have an address"))
        End If

        If String.IsNullOrEmpty(Me.Email) Then
            Me.BrokenRules.Add(New BrokenRule("Email", "Email is a required value"))
        Else
            If Me.Email.Length > 50 Then
                Me.BrokenRules.Add(New BrokenRule("Email", "Email should be 50 chars or less"))
            End If
        End If

        If String.IsNullOrEmpty(Me.Name) Then
            Me.BrokenRules.Add(New BrokenRule("Name", "Name is a required value"))
        Else
            If Me.Name.Length > 50 Then
                Me.BrokenRules.Add(New BrokenRule("Name", "Name should be 50 chars or less"))
            End If
        End If

    End Sub

End Class